﻿//Author:Michal Sporna
//License: https://operationsneakpeek.codeplex.com/license

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Screens
{
    /// <summary>
    /// level selector
    /// THIS CLASS IS NOT USED ANYMORE
    /// </summary>
    public static class LevelSelector
    {
        #region properties

        public static List<Classes.LevelSelectorItem> Items { get; set; }

        #endregion

        #region methods

        /// <summary>
        /// draw levels of the map
        /// </summary>
        public static void Draw()
        {
            //declare
            Classes.LevelSelectorItem lsItem;

            try
            {

                #region draw background first
                Util.Cache.Draw(Util.Cache.GetTexture("LevelSelectorBackground").Text2D, new Rectangle(0, 0, Util.Cache.GraphicsDevice.Viewport.Width, Util.Cache.GraphicsDevice.Viewport.Height), Color.White) ;

                #endregion

                for (int i = 0; i < Levels.LevelManager.CurrentMap.MapCells.Count; i++)
                {
                    //new instance of lsItem
                    lsItem = new Classes.LevelSelectorItem();
                    lsItem.Rectangle=new Rectangle(72, 72, 115,115);
                    //frame
                    Util.Cache.Draw(Util.Cache.GetTexture("LevelSelectorFrame").Text2D, new Rectangle(70, 70, 120, 120), Color.White);
                    //draw an icon of the map within the frame
                    Util.Cache.Draw(Util.Cache.GetTexture(Levels.LevelManager.CurrentMap.MapCells[i].ID + "_Icon").Text2D, lsItem.Rectangle, Color.White);

                    //assign id of the map cell
                    lsItem.MapCellID = Levels.LevelManager.CurrentMap.MapCells[i].ID;

                    //add to the collection
                    Items.Add(lsItem);
                }


            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return;
            }
        }


        /// <summary>
        /// check if level has been chosen
        /// if yes, set current map cell
        /// </summary>
        /// <param name="tapPos"></param>
        public static bool IsLevelChosen(Vector2 tapPos)
        {
         
            try
            {
                for (int i = 0; i < Items.Count; i++)
                {
                    if ((tapPos.X > Items[i].Rectangle.Left && tapPos.X < Items[i].Rectangle.Left + Items[i].Rectangle.Width) && (tapPos.Y > Items[i].Rectangle.Top && tapPos.Y < Items[i].Rectangle.Top + Items[i].Rectangle.Height))
                    {
                      
                        //loop thru all map cells and select the one, current one
                        for (int i1 = 0; i1 < Levels.LevelManager.CurrentMap.MapCells.Count; i1++)
                        {
                            if (Levels.LevelManager.CurrentMap.MapCells[i1].ID == Items[i].MapCellID)
                            {
                                //assign as current map cell
                                Levels.LevelManager.CurrentMap.CurrentMapCell = Levels.LevelManager.CurrentMap.MapCells[i1];
                                return true;
                            }
                        }

                        break;
                    }

                }


                return false;



            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return false;
            }
        }


        #endregion


    }
}
